package com.jeecg.struts.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import com.jeecg.springmvc.bean.ControlForm;


public class ReflectHelper {

	
	
	public static String checkUrlMethod(HttpServletRequest req,Map<String,List<ControlForm>> annotationMap ) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, SecurityException, NoSuchMethodException{
		
		// 请求url
		String reqUrl = req.getServletPath();
		String reqMethodName = req.getParameter("method");
		String reqParams = req.getQueryString();
		System.out.println("请求url:"+reqUrl);
		System.out.println("请求方法:"+reqMethodName);
		System.out.println("请求参数:"+reqParams);
		
		reqParams = reqParams.substring(0, reqParams.indexOf("&method="));
		String[] split = reqParams.split("&");
		Map<String,String> reqParamMap = new HashMap<String,String>();
		if(split!=null && split.length>0){
			for (String string : split) {
				String[] split2 = string.split("=");
				if(split2!=null && split2.length>=2){
					reqParamMap.put(split2[0], split2[1]);
				}
			}
		}
		
		List<ControlForm> annotationClassMap = annotationMap.get(reqUrl);
		if(annotationClassMap!=null && annotationClassMap.size()>0){
			boolean flag =false;
			
			for (ControlForm controlForm2 : annotationClassMap) {
				Class<?> className = Class.forName(controlForm2.getClassName());
				Map<String, Map<String, String>> methodParamNameMap = controlForm2.getMethodParamNameMap();
				//查找方法是否存在
				Map<String, String> paramMap = methodParamNameMap.get(reqMethodName);
				if(paramMap!=null && paramMap.size()>0){
					flag =  true;
					Set<String> parameKeySet = paramMap.keySet();
					Class [] paramArry = new Class[paramMap.size()];
					Object [] paramValueArry = new Object[paramMap.size()];
					int index =0;
					for (Iterator iterator = parameKeySet.iterator(); iterator.hasNext();) {
						//参数名称
						String paramName = (String) iterator.next();
						//参数类型
						String parameType = paramMap.get(paramName);
						//参数值
						String reqParamValue = reqParamMap.get(paramName);
							 //将请求过来的参数值 赋值给方法的参数
							System.out.println("方法参数名称:");
							try {
								Object paramValue = reflectObj(parameType,reqParamValue,reqParamMap);
								paramArry[index]= Class.forName(parameType);
								paramValueArry[index] = paramValue;
								index++;
							} catch (ClassNotFoundException e) {
								e.printStackTrace();
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					//调用请求方法
					Method addMethod = className.getMethod(reqMethodName,paramArry );
				    String resultStr =(String) addMethod.invoke(className.newInstance(), paramValueArry);
				    return resultStr;
				}
			}
			if(!flag ){
				System.out.println("找不到对应方法");
				return "-1";
			}
		}else{
			System.out.println("找不到对应url");
		}
		return "0";
	}
	
	
	 public static <E> Object reflectObj(String className,String str,Map<String,String> reqParamMap) throws Exception{  
		 	Class<?> forName = Class.forName(className);
	       if(className.equals("java.lang.String") || className.equals("java.lang.Integer")){
	    	    Constructor con = forName.getConstructor(str.getClass());  
	    	    Object obj = con.newInstance(str);   
	    	    return   obj;
	       }else{
	    	   return getObj(className,reqParamMap);
	       }
	  }  
	
	
	
	public static  Object getObj(String formClassName,Map<String,String> reqParamMap){
		//获取form的class路径
		Object obj =null;
		try {
			Set<String> keySet = reqParamMap.keySet();
			for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
				String string = (String) iterator.next();
			}
			
			Class<?> forName = Class.forName(formClassName);
			obj =  forName.newInstance();
			Field[] declaredFields = forName.getDeclaredFields();
			for(Field field :declaredFields){
				field.setAccessible(true);//字段属 都为private,避免安全性检查，故必须进行此操作  
				System.out.println("field:"+ field.getName() );
				field.set(obj, reqParamMap.get(field.getName()));
				field.setAccessible(false);//字段属 都为private,避免安全性检查，故必须进行此操作  
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return obj;
	}
	
	
	
	
}








